package com.simple.datax.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.simple.datax.bo.IDataxDictionaryBO;
import com.simple.datax.bo.IDataxParseRawMsgBO;
import com.simple.datax.bo.IDataxProcingBO;
import com.simple.datax.vo.DataxParseRawMsgVO;
import com.simple.datax.vo.DataxProcingVO;
import com.simple.util.common.Constants;
import com.simple.util.common.ErrorUtil;
import com.simple.util.common.ResentMessageServiceUtil;
import com.simple.util.exception.SimpleSystemException;
import com.simple.util.object.JFValidationBean;
import com.simple.util.object.PageInfoBean;

@Controller
public class MessageController extends AbstractDataxBaseController {
	private final static String JSP_PATH = "_Message";

	@Autowired
	private IDataxProcingBO dataxProcingBO;
	@Autowired
	private IDataxParseRawMsgBO dataxParseRawMsgBO;
	@Autowired
	private IDataxDictionaryBO dataxDictionaryBO;

	@Override
	protected String getBaseJspPath() {
		return JSP_PATH;
	}

	@RequestMapping("/messageSendList")
	public String messageSendList(
			@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "fcRecvRawMsgId", required = false) String fcRecvRawMsgId,
			HttpServletRequest request, HttpServletResponse response) {

		final PageInfoBean pageInfoBean = new PageInfoBean(pageNo);
		final List<DataxProcingVO> dataxProcingVOList = dataxProcingBO
				.findDataxProcingVOByPage(pageInfoBean, fcRecvRawMsgId);
		
		final Map<String, String> catagoryMessageStatusMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGESTATUS);
		request.setAttribute("catagoryMessageStatusMap",
				catagoryMessageStatusMap);
		request.setAttribute("dataxProcingVOList", dataxProcingVOList);

		final String pageInfo = super.generatePageInfo(request, response,
				pageInfoBean, "goPage");
		request.setAttribute("pageInfo", pageInfo);
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("fcRecvRawMsgId", fcRecvRawMsgId);
		return getJspPath("message_send_list");
	}

	@RequestMapping("/messageInfo")
	public String messageInfo(
			@RequestParam(value = "fcRecvRawMsgIdInfo", required = false) String fcRecvRawMsgIdInfo,
			HttpServletRequest request, HttpServletResponse response) {

		final List<JFValidationBean> errorList = getErrorList();
		final Map<String, String> catagoryMessageTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGETYPE);
		final Map<String, String> catagoryOperationTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_OPERATIONTYPE);
		final Map<String, String> catagoryMessageStatusMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGESTATUS);
		final Map<String, String> catagoryInformationTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_INFORMATIONTYPE);

		request.setAttribute("catagoryMessageTypeMap", catagoryMessageTypeMap);
		request.setAttribute("catagoryOperationTypeMap",
				catagoryOperationTypeMap);
		request.setAttribute("catagoryMessageStatusMap",
				catagoryMessageStatusMap);
		request.setAttribute("catagoryInformationTypeMap",
				catagoryInformationTypeMap);

		DataxParseRawMsgVO dataxParseRawMsgVO = dataxParseRawMsgBO
				.findById(fcRecvRawMsgIdInfo);
		request.setAttribute("dataxParseRawMsgVO", dataxParseRawMsgVO);

		if (dataxParseRawMsgVO == null) {
			errorList.add(new JFValidationBean(
					"message.messageerrorquery.error.not.analysis"));
		}
		return getJspPath("message_info");
	}

	@RequestMapping("/messageErrorQuery")
	public String messageErrorReSend(
			@RequestParam(value = "fcRecvRawMsgId", required = false, defaultValue = "") String fcRecvRawMsgId,
			HttpServletRequest request, HttpServletResponse response) {

		final List<JFValidationBean> errorList = getErrorList();
		final Map<String, String> catagoryMessageTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGETYPE);
		final Map<String, String> catagoryMessageStatusMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGESTATUS);
		
		DataxParseRawMsgVO dataxParseRawMsgVO = dataxParseRawMsgBO
				.findById(fcRecvRawMsgId);

		request.setAttribute("catagoryMessageStatusMap",
				catagoryMessageStatusMap);
		request.setAttribute("catagoryMessageTypeMap", catagoryMessageTypeMap);
		request.setAttribute("dataxParseRawMsgVO", dataxParseRawMsgVO);
		request.setAttribute("fcRecvRawMsgId", fcRecvRawMsgId);

		if (!"".equals(fcRecvRawMsgId) && dataxParseRawMsgVO == null) {
			errorList.add(new JFValidationBean(
					"message.messageerrorquery.error.not.analysis"));
		}

		return getJspPath("message_error_query");
	}

	@RequestMapping("/messageReSend")
	public String messageReSend(
			@RequestParam(value = "fcRecvRawMsgIdSelected", required = false) String fcRecvRawMsgIdSelected,
			HttpServletRequest request, HttpServletResponse response) {

		final List<JFValidationBean> errorList = getErrorList();
		boolean result = false;

		if (!"".equals(fcRecvRawMsgIdSelected)) {
			try{
				result = ResentMessageServiceUtil.resetProcess(fcRecvRawMsgIdSelected);
				if (result == false) {
					errorList.add(new JFValidationBean("message.system.failure"));
				} else {
					errorList.add(new JFValidationBean("message.system.sucess"));
				}
			}catch (SimpleSystemException ae) {
				errorList.add(ErrorUtil.generateJFValidationFromAppException(ae));
			}
		}
		
		return getJspPath("message_error_query");
	}
}
